package site.beanyon.now.aspect.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import site.beanyon.now.model.dto.TimePlanDTO;
import site.beanyon.now.model.po.TimePlanPO;
import site.beanyon.now.service.UserService;

import java.util.List;

@Aspect
@Component
class TimePlanLogAspect {
    private Logger logger = LoggerFactory.getLogger(TimePlanLogAspect.class);

    @Autowired
    private UserService userService;

    /**
     * 创建时间计划方法环绕通知
     *
     * @param jp 连接点
     */
    @Around("execution(* site.beanyon.now.service.TimePlanService.createTimePlan(..))")
    private void createTimePlan(ProceedingJoinPoint jp) throws Throwable {
        // 创建前
        TimePlanPO timePlanPO = (TimePlanPO) jp.getArgs()[0];

        try {
            jp.proceed();
        } catch (Throwable throwable) {
            // 创建异常
            logger.error("[error] create time plan, user_id=" + timePlanPO.getUserId()
                    + ", activity=" + timePlanPO.getActivity()
                    + ", status=" + timePlanPO.getStatus());
            throw throwable;
        }

        // 创建后
        logger.info("[success] create time plan, user_id=" + timePlanPO.getUserId()
                + ", activity=" + timePlanPO.getActivity()
                + ", status=" + timePlanPO.getStatus());
    }

    /**
     * 删除时间计划方法环绕通知
     *
     * @param jp 连接点
     */
    @Around("execution(* site.beanyon.now.service.TimePlanService.deleteTimePlan(..))")
    private boolean deleteTimePlan(ProceedingJoinPoint jp) throws Throwable {
        // 删除前
        Integer timePlanId = (Integer) jp.getArgs()[0];
        Integer userId = this.userService.getCurrentUser().getId();

        boolean result = false;

        try {
            result = (boolean) jp.proceed();
        } catch (Throwable throwable) {
            // 删除异常
            logger.error("[error] delete time plan, user_id=" + userId
                    + ", time_plan_id=" + timePlanId);
            throw throwable;
        }

        // 删除后
        logger.info("[success] delete time plan, user_id=" + userId
                + ", time_plan_id=" + timePlanId);

        return result;
    }

    /**
     * 获取时间计划列表方法环绕通知
     *
     * @param jp 连接点
     */
    @Around("execution(* site.beanyon.now.service.TimePlanService.listTimePlan(..))")
    private List<TimePlanDTO> listTimePlan(ProceedingJoinPoint jp) throws Throwable {
        // 获取前
        Integer userId = this.userService.getCurrentUser().getId();

        List<TimePlanDTO> timePlanDTOList = null;

        try {
            timePlanDTOList = (List<TimePlanDTO>) jp.proceed();
        } catch (Throwable throwable) {
            // 获取异常
            logger.error("[error] list time plan, user_id=" + userId);
            throw throwable;
        }

        // 获取后
        StringBuilder timePlanIds = new StringBuilder();
        for (TimePlanDTO timePlanDTO : timePlanDTOList) {
            timePlanIds.append(timePlanDTO.getId()).append(",");
        }
        logger.info("[success] list time plan, user_id=" + userId
                + ", time_plan_ids=" + timePlanIds);

        return timePlanDTOList;
    }
}
